{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 三大结构\n",
    "- 顺序\n",
    "- 分支\n",
    "- 循环"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分支\n",
    "- 分支的基本语法\n",
    "\n",
    "        if 条件表达式：\n",
    "            语句1\n",
    "            语句2\n",
    "            语句3\n",
    "            ......\n",
    "- 条件表达式就是计算结果必须为布尔值的表达式\n",
    "- 表达式后面的冒号不能少\n",
    "- 注意if后面的出现的语句，如果属于if语句块，则必须用一个缩进等级\n",
    "- 条件表达式结果为True执行if后面的缩进的语句块\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "去叫家长吧，孩纸\n",
      "我们不带你玩\n",
      "滚球的\n"
     ]
    }
   ],
   "source": [
    "# if语句练习\n",
    "# 如果age小于18岁，则打印信息“去叫家长”\n",
    "age = 17\n",
    "if age < 18:\n",
    "    print(\"去叫家长吧，孩纸\")\n",
    "    print(\"我们不带你玩\")\n",
    "    print(\"滚球的\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始上车喽，老司机们\n"
     ]
    }
   ],
   "source": [
    "# if语句练习\n",
    "# 如果age小于18岁，则打印信息“去叫家长”\n",
    "age = 19\n",
    "if age < 18:\n",
    "    print(\"去叫家长吧，孩纸\")\n",
    "    print(\"我们不带你玩\")\n",
    "    print(\"滚球的\")\n",
    "    \n",
    "print(\"开始上车喽，老司机们\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我们不带你玩\n",
      "滚球的\n",
      "开始上车喽，老司机们\n"
     ]
    }
   ],
   "source": [
    "# if语句练习\n",
    "# 如果age小于18岁，则打印信息“去叫家长”\n",
    "age = 19\n",
    "if age < 18:\n",
    "    print(\"去叫家长吧，孩纸\")\n",
    "print(\"我们不带你玩\")\n",
    "print(\"滚球的\")\n",
    "    \n",
    "print(\"开始上车喽，老司机们\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"今天学习for循环\")\n",
    "gender = \"男\"\n",
    "if gender == \"女\":\n",
    "    print(\"来，叔叔给你糖吃\")\n",
    "\n",
    "print(\"开始讲for循环了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 双向分支\n",
    "- if...else...语句\n",
    "\n",
    "           if 条件表达式\n",
    "               语句1\n",
    "               语句2\n",
    "               ...\n",
    "           else:\n",
    "               语句1\n",
    "               语句2\n",
    "               ...\n",
    "               \n",
    "- 双向分支有两个分支，当程序执行到if...else...语句的时候，一定会执行if或else中的一个，也仅执行一个\n",
    "- 缩进问题，if和else一个层级，其余语句一个层级"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入性别：nv\n",
      "你输入的性别是：nv\n",
      "发糖喽发糖喽\n",
      "你是女生，特殊照顾喽\n",
      "开始上课喽\n"
     ]
    }
   ],
   "source": [
    "# input的作用是\n",
    "# 1.在屏幕输出括号内的字符串\n",
    "# 2.接受用户输入的内容并返回到程序\n",
    "# 3.input返回的内容一定是字符串类型\n",
    "gender = input(\"请输入性别：\")\n",
    "print(\"你输入的性别是：{0}\".format(gender))\n",
    "\n",
    "if gender == \"nan\":\n",
    "    print(\"来，我们纪念一下今天吧，代码敲十遍\")\n",
    "else:\n",
    "    print(\"发糖喽发糖喽\")\n",
    "    print(\"你是女生，特殊照顾喽\")\n",
    "        \n",
    "print(\"开始上课喽\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 考试成绩判断\n",
    "# 90以上：输出优秀\n",
    "# 80-90：良\n",
    "# 70-80：中\n",
    "# 60-70：平\n",
    "# 60以下：输出：我没你这撒学僧\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入学生成绩：45\n",
      "起开，我没你这撒学僧\n"
     ]
    }
   ],
   "source": [
    "# score 存放学生成绩\n",
    "# 注意input的返回值类型 是字符串\n",
    "score = input(\"请输入学生成绩：\")\n",
    "# 需要把str转换成int\n",
    "score = int(score)\n",
    "\n",
    "if score>=90:\n",
    "    print(\"A\")\n",
    "if score>=80 and score<90:\n",
    "    print(\"B\")\n",
    "if score>=70 and score<80:\n",
    "    print(\"C\")\n",
    "if score>=60 and score<70:\n",
    "    print(\"D\")\n",
    "if score<60:\n",
    "    print(\"起开，我没你这撒学僧\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多路分支\n",
    "- 很多分支的情况，简称多路分支\n",
    "\n",
    "        if 条件表达式\n",
    "           语句1\n",
    "           ...\n",
    "        elif 条件表达式：\n",
    "           语句1\n",
    "           ...\n",
    "        elif 条件表达式\n",
    "           语句1\n",
    "           ...\n",
    "        .....\n",
    "        else:\n",
    "           语句1\n",
    "           ...\n",
    "- elif 可以有很多个\n",
    "- else可选\n",
    "- 多路分支只会选一个执行\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入学生成绩：45\n",
      "起开，我没你这撒学僧\n"
     ]
    }
   ],
   "source": [
    "# score 存放学生成绩\n",
    "# 注意input的返回值类型 是字符串\n",
    "score = input(\"请输入学生成绩：\")\n",
    "# 需要把str转换成int\n",
    "score = int(score)\n",
    "\n",
    "if score>=90:\n",
    "    print(\"A\")\n",
    "elif score>=80:\n",
    "    print(\"B\")\n",
    "elif score>=70:\n",
    "    print(\"C\")\n",
    "elif score>=60:\n",
    "    print(\"D\")\n",
    "else:\n",
    "    print(\"起开，我没你这撒学僧\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## if语句其他：\n",
    "- if语句可以嵌套使用，但不推荐\n",
    "- python没有switch-case语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环语句\n",
    "- 重复执行某些固定动作或者处理基本固定的事物\n",
    "- 分类\n",
    "    - for循环\n",
    "    - while循环\n",
    "## for 循环\n",
    "\n",
    "\n",
    "        for 变量 in 序列：\n",
    "            语句1\n",
    "            语句2\n",
    "            ...\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan\n",
      "lisi\n",
      "wangwu\n",
      "jingjing\n"
     ]
    }
   ],
   "source": [
    "# 列表就是一列数字或者其他值，一般用中括号表示\n",
    "# 例如 ['zhangsan','lisi','wangwu','jingjing']\n",
    "\n",
    "# 打印学生列表姓名\n",
    "for name in ['zhangsan','lisi','wangwu','jingjing']:\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan\n",
      "同学我们不约，不约，同学请自重\n",
      "lisi\n",
      "同学我们不约，不约，同学请自重\n",
      "wangwu\n",
      "同学我们不约，不约，同学请自重\n",
      "jingjing\n",
      "我的最爱jingjing出现了\n"
     ]
    }
   ],
   "source": [
    "# 打印学生列表姓名\n",
    "# 如果是jingjing，那肯定是我的最爱啊\n",
    "# 如果是别的学生，那要冷酷的拒绝他\n",
    "for name in ['zhangsan','lisi','wangwu','jingjing']:\n",
    "    print(name)\n",
    "    if name == \"jingjing\":\n",
    "        print(\"我的最爱{0}出现了\".format(name))\n",
    "    else:\n",
    "        print(\"同学我们不约，不约，同学请自重\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## range介绍\n",
    "- 生成一个数字序列\n",
    "- 具体范围可以设定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "# range练习\n",
    "# 打印从1-10的数字\n",
    "# 注意，一般在python中，如果有表示数字范围的两个数，一般是包含左边数字，不包含右边数字\n",
    "#randint是特例，他左右都包含\n",
    "# range函数在python2 和python3中严重区别\n",
    "for i in range(1,11):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for-else语句\n",
    "- 当for循环结束的时候，会执行else语句\n",
    "- else语句是可选语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhangsan\n",
      "同学我们不约，不约，同学请自重\n",
      "lisi\n",
      "同学我们不约，不约，同学请自重\n",
      "wangwu\n",
      "同学我们不约，不约，同学请自重\n",
      "jingjing\n",
      "我的最爱jingjing出现了\n",
      "别的都不是我的学生，不会再爱了\n"
     ]
    }
   ],
   "source": [
    "# for-else语句\n",
    "# 打印列表中的同学\n",
    "# 如果没有在列表中，或者列表结束了，我们需要打印提示语句，表示不再爱了\n",
    "\n",
    "# 打印学生列表姓名\n",
    "# 如果是jingjing，那肯定是我的最爱啊\n",
    "# 如果是别的学生，那要冷酷的拒绝他\n",
    "for name in ['zhangsan','lisi','wangwu','jingjing']:\n",
    "    print(name)\n",
    "    if name == \"jingjing\":\n",
    "        print(\"我的最爱{0}出现了\".format(name))\n",
    "    else:\n",
    "        print(\"同学我们不约，不约，同学请自重\")\n",
    "else:\n",
    "    print(\"别的都不是我的学生，不会再爱了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for循环之break,continue,pass\n",
    "- break： 无条件结束整个循环，简称循环猝死\n",
    "- continue： 无条件结束本次循环，从新进入下一轮循环\n",
    "- lass： 表示略过，通常用于占位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "我找到了\n"
     ]
    }
   ],
   "source": [
    "# 在数字1-10中，寻找数字7,一旦找到，打印出来，其余则什么都不做\n",
    "# for 循环中的变量表示，一般用i，k，m，n，或者indx，idx，item之类\n",
    "# 在pythno中，如果循环变量名称不重要，可以用下划线（_）代替\n",
    "\n",
    "for i in range(1,11):\n",
    "    if i == 7:\n",
    "        print(\"我找到了\")\n",
    "        break\n",
    "    else:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2是偶数\n",
      "4是偶数\n",
      "6是偶数\n",
      "8是偶数\n",
      "10是偶数\n"
     ]
    }
   ],
   "source": [
    "#continue语句联系\n",
    "# 在数字1-10中，寻找所有偶数，找到偶数后打印偶数\n",
    "\n",
    "\n",
    "for i in range(1,11):\n",
    "    if i%2 == 1:\n",
    "        continue\n",
    "    else:\n",
    "        print(\"{0}是偶数\".format(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2是偶数\n",
      "4是偶数\n",
      "6是偶数\n",
      "8是偶数\n",
      "10是偶数\n"
     ]
    }
   ],
   "source": [
    "#continue语句联系\n",
    "# 在数字1-10中，寻找所有偶数，找到偶数后打印偶数\n",
    "\n",
    "\n",
    "for i in range(1,11):\n",
    "    if i%2 == 0:\n",
    "        print(\"{0}是偶数\".format(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2是偶数\n",
      "4是偶数\n",
      "6是偶数\n",
      "8是偶数\n",
      "10是偶数\n"
     ]
    }
   ],
   "source": [
    "#continue语句联系\n",
    "# 在数字1-10中，寻找所有偶数，找到偶数后打印偶数\n",
    "# 本案例完整显示continue的用法和作用\n",
    "\n",
    "for i in range(1,11):\n",
    "    if i%2 == 1:\n",
    "        continue\n",
    "        \n",
    "    print(\"{0}是偶数\".format(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wo zai zhe li\n",
      "wo zai zhe li\n",
      "wo zai zhe li\n",
      "wo zai zhe li\n",
      "wo zai zhe li\n",
      "wo zai zhe li\n",
      "wo zai zhe li\n",
      "wo zai zhe li\n",
      "wo zai zhe li\n",
      "wo zai zhe li\n"
     ]
    }
   ],
   "source": [
    "# pass例子，一般用于占位\n",
    "# pass没有跳过功能\n",
    "\n",
    "for i in range(1,11):\n",
    "    pass\n",
    "    print(\"wo zai zhe li\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
